package com.jiang.controller;

import com.alibaba.fastjson.JSON;
import com.jiang.bean.QueryInfo;
import com.jiang.bean.Student;
import com.jiang.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;


@RestController
public class StuController {

    @Autowired
    StuService stuService;


    @RequestMapping("/allStu")
    public String getUserList(QueryInfo queryInfo){
        System.out.println(queryInfo);
        int numbers = stuService.getStuCounts("%" + queryInfo.getQuery() + "%");
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Student> Stus = stuService.getAllStu("%" + queryInfo.getQuery() + "%", pageStart, queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",Stus);
        System.out.println("总条数："+numbers);
        String s = JSON.toJSONString(res);
        return s;
    }


    @RequestMapping("/addStu")
    public String addStu(@RequestBody Student student){
        System.out.println(student);
        int i = stuService.addStu(student);
        String str = i >0 ? "success" : "error";
        return str;
    }



    @RequestMapping("/deleteStu")
    @CrossOrigin()
    public String deleteStu(int id){
        int i = stuService.deleteStu(id);
        String str = i > 0 ? "success" : "error";
        return str;
    }



    @RequestMapping(value = "/getStuUpdate",produces = {"application/json;charset=UTF-8"})
    public String getUpdateUser(int id) {
        Student updateUser = stuService.getUpdateStu(id);
        String users_json = JSON.toJSONString(updateUser);
        return users_json;
    }



    @RequestMapping("/updataStu")
    public String updateStu(@RequestBody Student student){
        int i = stuService.updateStu(student);
        String str = i > 0 ? "success" : "error";
        System.out.println(str);
        return str;
    }


}
